const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const productionGzipExtensions = ['js', 'css'];
const isProduction = process.env.NODE_ENV === 'production';

module.exports = {
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'less',
      //这里是你的定义的全局less 变量
      patterns: [
        path.resolve(__dirname, "src/assets/css/variable.less"),
      ]
    }
  },
  lintOnSave: false,
  chainWebpack: config => {
    config.module
      .rule('images')
      .use('image-webpack-loader')
      .loader('image-webpack-loader')
      .options({
        bypassOnDebug: true
      })
      .end()
  },
  configureWebpack: config => {
    if(isProduction){
      //为生产环境修改配置...
      config.optimization = {
        minimize: true,
        minimizer: [
          new TerserPlugin({
            sourceMap: false, // Must be set to true if using source-maps in production
            terserOptions: {
              compress: {
                drop_console: true,
                drop_debugger: true
              },
            },
          }),
        ],
        // 公共代码抽离
        splitChunks: {
          cacheGroups: {
            vendor: {
              chunks: "all",
              test: /node_modules/,
              name: "vendor",
              minChunks: 1,
              maxInitialRequests: 5,
              minSize: 0,
              priority: 100,
            },
            common: {
              chunks: "all",
              test: /[\\/]src[\\/]js[\\/]/,
              name: "common",
              minChunks: 2,
              maxInitialRequests: 5,
              minSize: 0,
              priority: 60
            },
            styles: {
              name: 'styles',
              test: /\.(sa|sc|c)ss$/,
              chunks: 'all',
              enforce: true,
            },
            runtimeChunk: {
              name: 'manifest'
            }
          }
        }
      };
      config.plugins.push(
        // 配置compression-webpack-plugin压缩
        new CompressionWebpackPlugin({
          algorithm: 'gzip',
          test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
          threshold: 10240,
          minRatio: 0.8
        })
      )
    }else {
      // 为开发环境修改配置...
    }
  },
};
